import md5 from 'crypto-js/md5';//加密
export default {
    data() {
      return{
          data:[],
          limit:10,
          page:1,
          title:10
      }
    },
    methods: {
      confirm () {
          this.$Modal.confirm({
              title: '确认删除？',
              content: '是否确认删除',
              onOk: () => {
                  if(this.selectData&&this.selectData.length>0){
                    this.manyDelete();
                  }else{
                    this.$Message.error('删除目标未选定'); 
                  }
                  
              },
              onCancel: () => {
                  this.$Message.info('取消删除');
              }
          });
      },
      show (index) {
          this.$Modal.info({
              title: 'User Info',
              content: `Name：${this.data[index].name}<br>Password：${this.data[index].password}<br>Email:${this.data[index].email}<br>Age：${this.data[index].age}<br>Data：${this.data[index].data}`
          })
      },
      remove (index) {
          var id=this.data[index]._id;
          if(this.data.length==1&&this.page!=1){
              this.page-=1
          }
          this.$http.delete(`http://10.31.160.22:3000/${this.module}/data/${id}`)
           .then(res=>{
               this.$Modal.success({
              title: 'delete success',
              content: `${res}`
          })
            this.getUserDate();
           });
      },
      search(){
          this.realSearchText=this.searchText;
          this.page=1;
          this.getUserDate();
      },
      pageChange(event){ 
          console.log(event);  
          this.page=event;
              this.getUserDate();
      },
      pageSizeChange(event){
          this.limit=event;
          this.getUserDate();
  
      },
      getUserDate(){
          this.$http.post(
              `http://10.31.160.22:3000/${this.module}/list`,{
              name:this.realSearchText,limit:this.limit,page:this.page
          }).then(res=>{
              this.data=res.data.docs;
              this.title=res.data.total;
              this.page=res.data.page;
              console.log(res.data);
          });  
      },
      manyDelete(){
          var selectDataID=[];
          for(var i=0;i<this.selectData.length;i++){
              selectDataID.push(this.selectData[i]._id);
          }
          console.log(selectDataID);
          if(this.data.length==selectDataID.length&&this.page!=1){
              this.page-=1
          }
          this.$http.post(`http://10.31.160.22:3000/${this.module}/manydata/delete`,{id:selectDataID}).then(res=>{
                this.$Modal.success({
              title: 'delete success',
              content: `${res}`
          })
            this.getUserDate();
           });
          
      },
      addSelectData(event){
        this.selectData=event;
      },
      handleSubmit (name) {
          this.$refs[name].validate((valid,value) => {
              if (valid) {
                if(this.formValidate.password){//密码加密
                    this.formValidate.password=md5(this.formValidate.password).toString();
                  }
                  if(this.type=="create"){
                  this.$http.post(`http://10.31.160.22:3000/${this.module}/data`,this.formValidate).then(res=>{
                  this.$Message.success('添加数据成功');
                  this.handleReset(name)
                  console.log(res);
                  this.modal=false;
                  this.getUserDate();
                  }
                  )
                  }
                  if(this.type=="change"){
                      console.log(this.formValidate);
                      this.$http.put(`http://10.31.160.22:3000/${this.module}/data/${this.formValidate._id}`,this.formValidate).then(res=>{
                  this.$Message.success("修改成功");
                  this.modal=false;
                  this.getUserDate();
                  }
                  )
                  }
              } else {
                  this.$Message.error('Fail!');
              }
          })
  
      },
      handleReset (name) {
          this.$refs[name].resetFields();
      }
    },
    created() {
      this.getUserDate();
    }
  };
  